<?php

/**
 * Admincp页面基类
 *
 * @author Alen
 * @since 1.0.0 2013-10-3 sutroon
 */
class AdmincpAction extends SoAction
{

    /**
     * 构造函数
     */
    public function __construct()
    {
        parent::__construct();
        if ($this->MODULE_NAME != 'Login')
        {
            // 验证登录
            $this->check_user_login();
        }

        // 初始化UI构造器
        $this->uig = new SoUIGeneratorForEaryUI();
        $this->assign_page_head('');

        $this->assign('table','');
        $this->assign('form','');
    }

    /**
     * 检测用户是否登录,如果没有登录则跳转到登录页面
     * 
     * @since 1.0.0 2013-10-8 sutroon
     */
    private function check_user_login()
    {
        $user = new SoBizUser(true);
        $info = $user->get_login_info();
        if (!$info)
        {
            redirect('请登录...', '?m=Login.html');
        }
        $this->assign('USER_NAME', $info['USER_NAME']);
    }

    /**
     * 设置页面信息
     * @param type $page_title
     * @param type $keywords
     * @param type $description
     * @param type $title_suffix
     * @since 1.0.0 2013-10-8 sutroon
     */
    protected function assign_page_head($page_title, $keywords = '', $description = '', $title_suffix = '')
    {
        $biz = new SoBizGeneral();
        $data = $biz->get_site_info();
        if ($data)
        {
            // 如果站点状态是关闭，则显示网站关闭消息
            if (get_array_value($data, 'SITE_STATE') == '0')
                exit(get_array_value($data, 'SITE_CLOSE_MESSAGE'));
            // 页面标题为空的话读取后台配置信息
            if (!$page_title)
            {
                $page_title = get_array_value($data, 'SITE_TITLE');
                $keywords = get_array_value($data, 'SITE_KEYWORDS');
                $description = get_array_value($data, 'SITE_DESCRIPTION');
                $title_suffix = get_array_value($data, 'SITE_TITLE_SUFFIX');
            }
        }
        $this->assign('page_title', $page_title);
        $this->assign('page_title_suffix', $title_suffix);
        $this->assign('meta_keywords', $keywords);
        $this->assign('meta_description', $description);
    }

    /**
     * 设置面包屑导航
     * @param type $arr
     * @example set_breadcrumb(array('文章管理'=>'?m=Article_List','编辑'=>'###'));
     * @since 
     *      1.0.0 2013-9-30 sutroon
     *      1.1.0 2014-3-7 sutroon 新增自动索引功能
     */
    protected function assign_breadcrumb($arr = null)
    {
        $this->assign('breadcrumb', $this->uig->get_breadcrumb($arr));
    }

    /**
     * 面包屑导航数据缓存
     * @var type 
     */
    private $arr_breadcrumb;

    /**
     * 开始面包屑导航
     * @since 1.0 2014-3-7 by sutroon
     */
    protected function begin_breadcrumb()
    {
        $this->arr_breadcrumb = array('首页' => '?m=index.html');
    }

    /**
     * 添加面包屑导航节点
     * @param type $name
     * @param type $url
     */
    protected function add_breadcrumb($name, $url)
    {
        $this->arr_breadcrumb[$name] = $url;
    }

    /**
     * 结束面包屑导航
     */
    protected function end_breadcrumb()
    {
        $this->assign_breadcrumb($this->arr_breadcrumb);
    }

    /**
     * 
     * @param type $url
     * @param type $toolbarId
     * @param type $showCheckBox
     * @since 1.0 2014-3-7 by sutroon
     */
    protected function begin_table($url = '', $id = 'dg', $toolbarId = 'tb', $showCheckBox = true, $paging = true)
    {
        $str = ' id="' . $id . '" class="easyui-datagrid" data-options="rownumbers:true,url:\'' . $url . '\',method:\'get\'';
        if ($showCheckBox)
            $str.=',singleSelect:true,selectOnCheck:true,checkOnSelect:true';
        if ($paging)
            $str.=', pagination:true';
        if ($toolbarId)
        {
            $str.=',toolbar:\'#' . $toolbarId . '\'';
            $this->table_toolbar = '<div id="' . $toolbarId . '" style="padding:5px;height:auto">';
        }
        $str.='"';
        $this->uig->begin_table($str);
    }

    protected function add_head($arr)
    {
        $this->uig->add_thead2($arr);
    }

    private $table_toolbar;
    private $table_toolbar_btns;
    private $table_toolbar_search;

    protected function begin_table_button()
    {
        $this->table_toolbar_btns = '<div style="margin-bottom:5px">';
    }

    /**
     * 添加CURD表格按钮
     * @param type $type 支持:add | edit | save | cut | remove
     * @param type $text
     * @param type $action
     */
    protected function add_table_button($type, $text, $href = '#',$click='')
    {
        if(!$click){
            switch($type){
                case 'add':
                    $click='newItem();';
                    break;
                case 'edit':
                    $click='editItem()';
                    break;
                case 'delete':
                    $click='destoryItem()';
                    break;
            }
        }
        $this->table_toolbar_btns.='<a href="' . $href . '" class="easyui-linkbutton" iconCls="icon-' . $type . '" plain="true" onclick="'.$click.'">' . $text . '</a>';
        //$this->table_toolbar_btns.='<a href="#" class="easyui-linkbutton" iconCls="icon-' . $type . '" plain="true" onclick="addTabMain(\'' . $text . '\',\'' . $href . '\');">' . $text . '</a>';
    }

    protected function end_table_button()
    {
        $this->table_toolbar_btns.='</div>';
    }

    protected function begin_table_search()
    {
        $this->table_toolbar_search = '<div><form method="post">';
    }

    protected function add_table_search_input($label, $control, $ln = '')
    {
        $this->table_toolbar_search.=$label . $control . $ln;
    }

    protected function add_table_search_text($label, $id)
    {
        $this->table_toolbar_search.=$label . '：<input id="' . $id . '" style="width:80px" />';
    }

    protected function add_table_search_date($label, $id)
    {
        $this->table_toolbar_search.=$label . '：<input id="' . $id . '" class="easyui-datebox" style="width:80px" />';
    }

    protected function add_table_search_combobox($label, $id, $arr = null)
    {
        $str = '';
        if ($arr)
        {
            foreach ($arr as $v => $n)
            {
                $str.='<option value="' . $v . '">' . $n . '</option>';
            }
        }
        $this->table_toolbar_search.=$label . '：<select id="' . $id . '" class="easyui-combobox" panelHeight="auto" style="width:100px">' . $str . '</select>';
    }

    protected function end_table_search()
    {
        $this->table_toolbar_search.=' <a href="#" class="easyui-linkbutton" iconCls="icon-search" onclick="">搜索</a></form></div>';
    }

    protected function end_table()
    {
        if ($this->table_toolbar)
            $this->table_toolbar.=$this->table_toolbar_btns . $this->table_toolbar_search . '</div>';
        $this->uig->end_table();
        $this->assign('table', $this->uig->get_text() . $this->table_toolbar);
    }
    
    private $form_content;
    private $form_buttons;
    private $form_id;
    private $form_is_dialog;
    
    /**
     * 开始表单
     * 如果$title有内容则为dialog表单
     * @param type $id
     * @param type $action
     * @param type $title
     */
    protected function begin_form($id='frm',$action='',$title='编辑内容'){
        $this->form_id=$id;
        if($title){
            $this->form_is_dialog=TRUE;
            $this->form_content='<div id="dlg'.$id.'" class="easyui-dialog" style="width:640px;height:510px;padding:10px 20px" closed="true" buttons="#dlg'.$id.'-buttons"><div class="ftitle">'.$title.'</div>';
        }
        $this->form_content.='<form id="'.$id.'" method="post" '.($action?' action="'.$action.'"':'').'>';
    }
    protected  function end_form(){
        $this->form_content.='</form>'.($this->form_is_dialog?'</div>':'').$this->form_buttons;
        $this->assign('form',$this->form_content);        
    }
    protected function add_form_editor($label,$name,$value='',$rows=6){
        $this->form_content.='<div class="fitem"><label>'.$label.':</label><textarea name="'.$name.'" class="xeditor" rows="'.$rows.'">'.$value.'</textarea></div>';
    }
    protected function add_form_textbox($label,$name,$value='',$required=false){
        $this->form_content.='<div class="fitem"><label>'.$label.':</label><input name="'.$name.'" '.($required?'class="easyui-validatebox" required="true"':'').' value="'.$value.'" /></div>';
    }
    
    protected function add_form_numberbox($label,$name,$value='',$attr='min:10,max:90,precision:2,required:true'){
        $this->form_content.='<div class="fitem"><label>'.$label.':</label><input class="easyui-numberbox" data-options="'.$attr.'" value="'.$value.'" /></div>';
    }
    protected  function add_form_datebox($label,$name,$value=''){
        $this->form_content.='<div class="fitem"><label>'.$label.':</label><input class="easyui-datebox" value="'.$value.'" /></div>';
    }
    protected function add_form_datetimebox($label,$name,$value=''){
        $this->form_content.='<div class="fitem"><label>'.$label.':</label><input class="easyui-datetimebox" required style="width:150px" value="'.$value.'"></div>';
    }
    protected  function add_form_combobox($label,$name,$url){
        $this->form_content.='<div class="fitem"><label>'.$label.':</label><input class="easyui-combobox" id="language" name="language"   data-options="url: \'combobox_data2.json\', method: \'get\', valueField:\'value\', textField:\'text\', groupField:\'group\'" /></div>';
    }
    protected function add_form_button(){
        $this->form_buttons.='<div id="dlg'.$this->form_id.'-buttons">
        <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-ok" onclick="saveUser()">保存</a>
        <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-cancel" onclick="javascript:$(\'#dlg\').dialog(\'close\')">取消</a>
    </div>';
    }
    
        /*
         
            
            <div class="fitem">
                <label>Last Name:</label>
                <input name="lastname" class="easyui-validatebox" required="true">
            </div>
            <div class="fitem">
                <label>Phone:</label>
                <input name="phone">
            </div>
            <div class="fitem">
                <label>Email:</label>
                <input name="email" class="easyui-validatebox" validType="email">
            </div>
        
    
         */

    /**
     * 设置搜索栏 
     * @since 1.0.0 2013-12-6 sutroon
     */
    protected function assign_search()
    {
        $this->assign('search', $this->uig->get_search(request_string('keyword')));
    }

    /**
     * 设置带分页条的搜索栏
     * @param type $pager_text
     * @since 1.0.0 2013-12-6 sutroon
     */
    protected function assign_search_with_pager($pager_text)
    {
        $this->assign('search_with_pager', '<div class="row"><div class="col-lg-6">' . $pager_text . '</div><div class="col-lg-6">' . $this->uig->get_search(request_string('keyword')) . '</div></div>');
    }

    /**
     * 设置页面标题
     * @param type $h1
     * @param type $small
     * @example set_h1('文章管理','在这里管理所有文章');
     * @since 
     *      1.0.0 2013-9-30 sutroon
     *      1.1.0 2014-3-7 sutroon 新增自动索引功能
     */
    protected function assign_h1($h1 = '', $small = '', $panel = '')
    {
        $this->assign('pageheader', $this->uig->get_h1($h1, $small) . $panel);
    }

    /**
     * 获取编辑记录按钮
     * @param type $id
     * @param type $modname
     * @return type
     * @since 1.0.0 2013-10-3 sutroon
     * @example get_edit_linkbutton('article_edit',$row['ID']);
     */
    protected function get_edit_linkbutton($id, $modname)
    {
        return $this->uig->get_linkbutton('编辑', '?m=' . $modname . '&id=' . $id);
    }

    /**
     * 获取删除记录按钮
     * @param type $id
     * @param type $title
     * @return type
     * @since 1.0.0 2013-10-3 sutroon
     * @example get_delete_linkbutton($row['ID'],$row['Title']);
     */
    protected function get_delete_linkbutton($id, $title)
    {
        return $this->uig->get_linkbutton('删除', '###', 'id="#' . $id . '" title="' . $title . '" onclick="confirmADelete(this,\'?m=article_list&action=delete&id=.key.\')"');
    }

    /**
     * 获取编辑和删除记录等控制按钮
     * @param type $id
     * @param type $title
     * @param type $modname
     * @param type $btns
     * @param type $adduri 新增按钮的链接
     * @return string
     * @since 1.0.0 2013-10-3 sutroon
     */
    protected function get_control_linkbuttons($id, $title, $modname, $btns = array('edit', 'delete'), $adduri = '')
    {
        $str = '';
        foreach ($btns as $btn)
        {
            switch (strtolower($btn))
            {
                case 'edit':
                    $str.=$this->uig->get_linkbutton('编辑', '?m=' . $modname . '_Edit&id=' . $id) . ' ';
                    break;
                case 'delete':
                    $str.=$this->uig->get_linkbutton('删除', '###', 'id="#' . $id . '" title="' . $title . '" onclick="confirmADelete(this,\'?m=' . $modname . '_List&action=delete&id=.key.\')"') . ' ';
                    break;
                case 'add':
                    $str.=$this->uig->get_linkbutton('新增', $adduri ? $adduri : '?m=' . $modname) . ' ';
                    break;
                default :
                    $str.=$btn . ' ';
                    break;
            }
        }
        return $str;
    }

}

?>
